package com.lst.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.dao.ClientOnlineCourseMapper;
import com.lst.entity.request.OnlineCourseListReqPara;
import com.lst.entity.response.ActionVideoResPara;
import com.lst.model.ActionVideo;
import com.lst.model.ClientOnlineCourse;
import com.lst.model.OnlineCourse;
import com.lst.service.ActionVideoService;
import com.lst.service.OnlineCourseService;
import com.lst.utils.CommFunUtils;
import com.lst.utils.RequestUtils;


/**
 * @ClassName: CourseDetailServlet
 * @Description: 课程详情接口
 * 
 */
public class CourseDetailServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;

	private ActionVideoService actionVideoService = ctx.getBean(ActionVideoService.class);
	
	private OnlineCourseService onlineCourseService = ctx.getBean(OnlineCourseService.class);
	
	  @Autowired
	 private ClientOnlineCourseMapper clientOnlineCourseMapper = ctx.getBean(ClientOnlineCourseMapper.class);

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		ActionVideoResPara resPara = new ActionVideoResPara();
		try {
			OnlineCourseListReqPara reqPara = getReqPara(request);

			if (isCheckField(reqPara, resPara)) {
				    Integer ocid = Integer.valueOf(reqPara.getOcid());			
				    Integer userid = Integer.valueOf(reqPara.getUserid());
				    
				    OnlineCourse onlinecourse = onlineCourseService.selectByPrimaryKey(ocid);
				    //判断功夫是否添加
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("clientid", userid);
					map.put("courseid", ocid);

					List<ClientOnlineCourse> coclist = clientOnlineCourseMapper.queryList(map);
					if(coclist!=null && coclist.size()>0){
						onlinecourse.setIsAdd(true);
					}
					
				    resPara.setOnlineCourse(onlinecourse);
    					//动作列表
    					List<ActionVideo> actionList=actionVideoService.queryByList(ocid);
    					if(!actionList.isEmpty()){
    						for(ActionVideo actionVideo:actionList){
    							actionVideo.setActionPoints(CommFunUtils.formatStr(actionVideo.getActionPoints(), "• ", "\r\n"));
    						}
    					}
    					
    				    File f= new File(CommCode.readUrl +onlinecourse.getUrl().substring(13));  
    				    if (f.exists() && f.isFile()){  
    				    	resPara.setZiplength(f.length());
    				    }else{  
    				    	resPara.setZiplength(-1);  //文件不存在或者不是文件
    				    }
    					
    					resPara.setActionVideoList(actionList);
    					resPara.setCode(CommCode.M_Y000000);
        				resPara.setMessage(CommCode.M_Y000001);
                 
			}
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}
		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);
			logger.info(jsonResult);

		//	logger.info("CourseDetailServlet jsonResult: " + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

		//	logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public OnlineCourseListReqPara getReqPara(HttpServletRequest request) {

		OnlineCourseListReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request,new OnlineCourseListReqPara());

			reqPara = (OnlineCourseListReqPara) baseRequest.clone();

			String userid = request.getParameter("userid");
			String ocid = request.getParameter("ocid");

//			logger.info("reqPara userid: " + userid);
//			logger.info("reqPara ocid: " + ocid);

			reqPara.setUserid(userid);
			reqPara.setOcid(ocid);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}
		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		OnlineCourseListReqPara ocReqPara = (OnlineCourseListReqPara) reqPara;
		ActionVideoResPara ocResPara = (ActionVideoResPara) resPara;

		if (StringUtils.isEmpty(ocReqPara.getOcid())) {
			ocResPara.setCode(CommCode.M_A000000);
			ocResPara.setMessage(CommCode.M_S000011);
		} else {
			flag = true;
		}
		return flag;
	}
}
